package com.ljy.my_study.lintcode;
/** 
* @author James
* @date 2018年3月28日 
*/
public class LintCode {
	//计算数字k在0到n中的出现的次数，k可能是0~9的一个值
	public static int question1Answer1(int k,int n) {
		StringBuilder sb=new StringBuilder();
        for(int i=0;i<=n;i++){
            sb.append(i+"");
        }
        String s=sb.toString();
        int count=0;
        for(int i=0;i<s.length();i++){
            if(((k+"")).equals(s.substring(i,i+1))){
                count++;
            }
        }
        return count;
	}
	
	public static int question1Answer2(int k,int n) {
		int count=0;
		if(k>n) {
			return count;
		}
		if(n<=9) {
			return 1;
		}
		int digit=1;
		int a=n/10;
		int b=n%10;
		if(k<a&&a<=9) {
			count=10^1;
			count+=a-1;
		}else if(k==a) {
			count+=b;
			count+=a-1;
		}
		if(k<=b) {
			count+=1;
		}
		return count;
	}
	public static void main(String[] args) {
		question1Answer1(1,12);
	}
}
